﻿using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Installer.Packages;

namespace CharityGamesOnline.Modules.Game
{
    public class GameController : ISearchable
    {
        #region Public Methods

        /// <summary>
        /// gets all games module
        /// </summary>
        /// <returns></returns>
        public List<GamePackageInfo> GetGamePackages()
        {
            return CBO.FillCollection<GamePackageInfo>(DataProvider.Instance().GetGamePackages());
        }        

        public GamePackageInfo GetGamePackage(int packageID)
        {
            return (GamePackageInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetGamePackage(packageID), typeof(GamePackageInfo));
        }

        public List<GameInfo> GetGames()
        {
            return GetGames(0, 0);
        }

        public List<GameInfo> GetGames(int portalID,int eventID)
        {
            return CBO.FillCollection<GameInfo>(DataProvider.Instance().GetGames(portalID, eventID));
        }

        public GameInfo GetGame(int gameID)
        {
            return (GameInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetGame(gameID), typeof(GameInfo));
        }

        public void SaveGame(GameInfo e)
        {
            DataProvider.Instance().SaveGame(e.GameID, e.PortalID, e.LastUpdatedID, e.Status, e.Name, e.Description,
                                             e.ImageURL, e.ImageType, e.ImageWidth, e.ImageHeight,
                                             e.GameTabID, e.Category, e.ImageDisplay);
        }

        public void Delete(int gameID)
        {
            DataProvider.Instance().DeleteGame(gameID);
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            //SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            //List<GameInfo> colGames = GetGames(ModInfo.ModuleID);
            //foreach (GameInfo e in colGames)
            //{
            //    SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, e.Name, e.CreatedByUser, e.CreatedDate, ModInfo.ModuleID, e.ItemId.ToString(), e.Content, "ItemId=" + e.ItemId.ToString());
            //    SearchItemCollection.Add(SearchItem);
            //}

            //return SearchItemCollection;
            return null;

        }

        #endregion
    }


    public class GameEnrollmentController
    {
        #region Public Methods

        public GameEnrollmentInfo GetEnrollment(int portalID,int enrollmentID)
        {
            return (GameEnrollmentInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetEnrollment(portalID,enrollmentID), typeof(GameEnrollmentInfo));
        }

        public List<GameEnrollmentInfo> GetEnrollments(int portalID, int eventID, int gameID, int userID)
        {
            return GetEnrollments(portalID, eventID, gameID, userID, 0, false, 0, 0);
        }

        public List<GameEnrollmentInfo> GetEnrollments(int portalID, int eventID, int gameID, int userID, int enrollStatusID, 
                                                       bool getCount, int pageSize, int currentPage)
		{
            return CBO.FillCollection<GameEnrollmentInfo>(DataProvider.Instance().GetEnrollments(portalID, eventID, gameID, userID, enrollStatusID, getCount, pageSize, currentPage));
		}

        public List<GameEnrollmentInfo> GetLeaderBoard(int portalID, int eventID)
        {
            return GetEnrollments(portalID, eventID, 0, 0, (int)GameEnrollmentInfo.STATUS.COMPLETED, false, 0, 0);
        }

        public List<GameEnrollmentInfo> GetPlayedEvent(int portalID, int userID, bool getCount, int pageSize, int currentPage)
        {
            return CBO.FillCollection<GameEnrollmentInfo>(DataProvider.Instance().GetEnrollmentsByUser(portalID, userID, getCount, pageSize, currentPage));
        }

        public int SaveEnrollment(GameEnrollmentInfo e)
        {
            return (int)DataProvider.Instance().SaveEnrollment(e.EnrollmentID, e.PortalID, e.EventID, e.GameID, e.UserID, e.EnrollStatusID,
                e.EnrollFee, e.StartTime, e.EndTime, e.ConnectCount, e.Score);
        }
        
        public void DeleteEnrollment(int portalID,int enrollmentID)
        {
            DataProvider.Instance().DeleteEnrollment(portalID, enrollmentID);
        }

        /// <summary>
        /// Get game enroll count with status COMPLETED
        /// </summary>
        /// <param name="portalID"></param>
        /// <param name="eventID"></param>
        /// <returns></returns>
        public int GetPlayedCount(int portalID, int eventID)
        {
            return GetPlayedCount(portalID, eventID, 0, 0, (int)GameEnrollmentInfo.STATUS.COMPLETED);
        }

        public int GetPlayedCount(int portalID, int eventID, int gameID, int userID, int enrollStatusID)
        {
            return DataProvider.Instance().GetPlayedCount(portalID,eventID,gameID,userID,enrollStatusID);
        }

        public decimal GetMoneyCount(int portalID, int eventID)
        {
            return GetMoneyCount(portalID, eventID, 0, 0, 0);
        }

        public decimal GetMoneyCount(int portalID, int eventID, int gameID, int userID, int enrollStatusID)
        {
            return DataProvider.Instance().GetMoneyCount(portalID, eventID, gameID, userID, enrollStatusID);
        }

        public int GetUserRank(int enrollID, int userID)
        {
            return DataProvider.Instance().GetUserRank(enrollID, userID);
        }

        public int GetUncompletedEnrollID(int portalID, int eventID, int gameID, int userID)
        {
            List<GameEnrollmentInfo> lstEnroll = GetEnrollments(portalID, eventID, gameID, userID);
            int retEnrollID = 0;
            foreach (GameEnrollmentInfo objEnroll in lstEnroll)
            {
                if (objEnroll.EnrollStatusID != (int)GameEnrollmentInfo.STATUS.COMPLETED)
                {
                    retEnrollID = objEnroll.EnrollmentID;
                    break;
                }
            }

            return retEnrollID;
        }

        #endregion        
    }
}
